package com.tj.jinzao.widgets;

import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.tj.jinzao.R;


/**
 * ListView为空时显示
 * Created by wjy on 2015/4/23.
 */
public final class EmptyView extends LinearLayout {
    private State mState = State.LOADING;
    private View mLoadingView;
    private TextView mTextView;
    private ImageView mEmptyView;

    public static State[] stateValues;
    private String mEmptyString;
    private int mEmptyImageRes, mEmptyNetErrorRes;

    public enum State {
        LOADING, NO_DATA, SERVER_ERROR, NETWORK_ERROR
    }

    public EmptyView(Context context) {
        this(context, null);
    }

    public EmptyView(Context context, AttributeSet attrs) {
        super(context, attrs);
        if (null == stateValues) {
            stateValues = State.values();
        }
        LayoutInflater.from(context).inflate(R.layout.empty_view, this, true);

        mLoadingView = findViewById(R.id.empty_view_progress);
        mTextView = findViewById(R.id.empty_view_text);
        mEmptyView = findViewById(R.id.empty_view_image);
    }

    public void setTextColor(int color) {
        mTextView.setTextColor(color);
    }

    private void internalSetState() {
        switch (mState) {
            case LOADING:
                mLoadingView.setVisibility(VISIBLE);
                mTextView.setVisibility(GONE);
                mEmptyView.setVisibility(GONE);
                break;
            case NO_DATA:
                mLoadingView.setVisibility(GONE);
                mTextView.setVisibility(VISIBLE);
                mEmptyView.setVisibility(VISIBLE);
                mEmptyView.setImageResource(mEmptyImageRes != 0 ? mEmptyImageRes : R.mipmap.empty_view_no_data);
                mTextView.setText(mEmptyString);
                break;
            case SERVER_ERROR:
                mLoadingView.setVisibility(GONE);
                mTextView.setVisibility(VISIBLE);
                mEmptyView.setVisibility(VISIBLE);
                mEmptyView.setImageResource(mEmptyNetErrorRes != 0 ? mEmptyNetErrorRes : R.mipmap.empty_view_network_error);
                mTextView.setText(R.string.empty_net_error);
                break;
            case NETWORK_ERROR:
                mLoadingView.setVisibility(GONE);
                mTextView.setVisibility(VISIBLE);
                mEmptyView.setVisibility(VISIBLE);
                mEmptyView.setImageResource(mEmptyNetErrorRes != 0 ? mEmptyNetErrorRes : R.mipmap.empty_view_network_error);
                mTextView.setText(R.string.empty_net_error);
                break;
        }
    }

    public final State getState() {
        return mState;
    }

    public final void setState(State state, String empty, int imageRes) {
        if (state == State.NO_DATA) mEmptyImageRes = imageRes;
        else mEmptyNetErrorRes = imageRes;
        if (!TextUtils.equals(mEmptyString, empty) || (state != mState
                && (!(state == State.NO_DATA && (mState == State.NETWORK_ERROR
                || mState == State.SERVER_ERROR))))) {
            // If current state is NETWORK_ERROR or SERVER_ERROR,
            // we are not turning to NO_DATA
            mEmptyString = empty;
            mState = state;
            try {
                internalSetState();
            } catch (Exception ignored) {
            }
        }
    }

    public final void setState(State state, String empty) {
        setState(state, empty, 0);
    }

    public final void setState(State state, int imageRes) {
        setState(state, null, imageRes);
    }
}
